package algorithm

import (
	"fmt"
	"sync/atomic"
	"time"
)

var total int64

func GenPrime(ch chan<- int) {
	for i := 2; ; i++ {
		ch <- i
	}
}

func PrimeFilter(in <-chan int, out chan<- int, prime int) {
	gid := atomic.AddInt64(&total, 1)
	for v := range in {
		fmt.Printf("协程号[%d]处理 %dmod%d, in=%p, out=%p\n", gid, v, prime, in, out)
		if v%prime != 0 {
			out <- v
		}
		time.Sleep(1 * time.Second)
	}
}

func FindPrime() {
	ch := make(chan int)
	go GenPrime(ch)

	for i := 0; i < 10; i++ {
		prime := <-ch
		fmt.Println(prime)

		ch1 := make(chan int)
		fmt.Printf("ch=%p, ch1=%p\n", ch, ch1)
		go PrimeFilter(ch, ch1, prime)
		ch = ch1
	}
}
